package com.qunar.train.mock.util;


import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * @author: xudong.tai
 * @since: 2018/7/2 Description:
 **/
public class ClassAccessControl {

    //use reflection to remove the final modifier
    public static void resetFinalAccess(Class clazz, String fieldName) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            int modifiers = modifiersField.getInt(field);
            modifiers &= ~Modifier.FINAL;
            modifiersField.setInt(field, modifiers);
            //field.set(null, null);
            if (Modifier.isStatic(field.getModifiers())) {
                field.set(null, null);
            }
        } catch (Exception e) {
            AegisLog.error(e);
        }
    }

}
